package com.loren.demo07.cluster.websocket.service;

import com.alibaba.fastjson.JSONObject;
import com.loren.demo07.cluster.dto.ClusterMessage;
import com.loren.demo07.cluster.subscriber.ClusterMessageSubscriber;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class ClusterService {

    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 所有的消息都是发给Redis，Redis再通过WebSocket发送给客户端
     */
    public void sendTextMessage(ClusterMessage message) {
        this.stringRedisTemplate.convertAndSend(ClusterMessageSubscriber.CHANNEL_NAME, JSONObject.toJSONString(message));
    }
}
